package com.cars.elasticsearch.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.alibaba.fastjson2.JSON;
import com.cars.elasticsearch.Variable.GlobalVariables;
import com.cars.elasticsearch.domain.DO.Course;
import com.cars.elasticsearch.service.CourseService;
import com.cars.elasticsearch.util.elasticSearch.DocumentUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: caoqianfan
 * @date: 2023/10/16 15:13
 */
@Slf4j
@RestController
@RequestMapping("/QSCompare/course")
public class QuerySpeedComparison {

    @Resource
    private CourseService courseService;
    @Resource
    DocumentUtil documentUtil;

    /**
     * 比较es与mysql查询速度
     *
     * @param id id
     * @return {@code ResponseEntity<String>}
     */
    @GetMapping("list")
    public ResponseEntity<String> getById(@RequestParam(value = "id", required = false) Long id) {
        TimeInterval timer = DateUtil.timer();
        timer.start("1");
        if (id != null) {
            Course mysqlCourse = courseService.getById(id);
            log.info("<mysql>[getById]:{}", JSON.toJSONString(mysqlCourse));
        } else {
            List<Course> mysqlCourseList = courseService.list();
            log.info("<mysql>[list]:{}", JSON.toJSONString(mysqlCourseList));
        }
        timer.start("2");
        if (id != null) {
            Course course1 = new Course();
            course1.setId(id);
            Course esCourse = documentUtil.getDocument(course1, GlobalVariables.INDEX_NAME_COURSE);
            log.info("<es>[getDocument]:{}", JSON.toJSONString(esCourse));
        } else {
            List<Course> esCourseList = documentUtil.listDocument(GlobalVariables.INDEX_NAME_COURSE).stream().sorted(Comparator.comparing(Course::getId)).collect(Collectors.toList());
            log.info("<es>[listDocument]:{}", JSON.toJSONString(esCourseList));
        }
        return ResponseEntity.ok((id != null ? "[单表]-[单条数据]" : "[单表]-[全部数据]") + "查询数据库用时: " + timer.intervalMs("1") + "ms; 查询Elasticsearch用时: " + timer.intervalMs("2") + "ms.");
    }

    @PostMapping("/save")
    public ResponseEntity<String> save(@RequestBody List<Course> courseList) {
        TimeInterval timer = DateUtil.timer();
        timer.start("1");
        courseService.saveBatch(courseList);
        timer.start("2");
        documentUtil.batchAddDocument(courseList, GlobalVariables.INDEX_NAME_COURSE);
        return ResponseEntity.ok("数据库插入用时: " + timer.intervalMs("1") + "ms; ES插入用时: " + timer.intervalMs("2") + "ms.");
    }

    @PutMapping("/update")
    public ResponseEntity<String> update(@RequestBody List<Course> courseList) {
        TimeInterval timer = DateUtil.timer();
        timer.start("1");
        courseService.updateBatchById(courseList);
        timer.start("2");
        documentUtil.batchUpdateDocument(courseList, GlobalVariables.INDEX_NAME_COURSE);
        return ResponseEntity.ok("数据库更新用时: " + timer.intervalMs("1") + "ms; ES更新用时: " + timer.intervalMs("2") + "ms.");
    }

    @DeleteMapping("/delete")
    public ResponseEntity<String> delete(@RequestBody List<Long> idList) {
        TimeInterval timer = DateUtil.timer();
        timer.start("1");
        courseService.removeBatchByIds(idList);
        timer.start("2");
        List<Course> courseList = new ArrayList<>();
        for (Long id : idList) {
            Course course = new Course();
            course.setId(id);
            courseList.add(course);
        }
        documentUtil.batchDeleteDocument(courseList, GlobalVariables.INDEX_NAME_COURSE);
        return ResponseEntity.ok("数据库删除用时: " + timer.intervalMs("1") + "ms; ES删除用时: " + timer.intervalMs("2") + "ms.");
    }
}